/**
 * 
 */
package com.devmindset.legoframework.client.screen;

import com.devmindset.legoframework.client.mvc.LegoComponent;
import com.google.gwt.user.client.Timer;
import com.smartgwt.client.types.AnimationEffect;
import com.smartgwt.client.widgets.AnimationCallback;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.layout.Layout;

/**
 * Placeholder for the {@link LegoComponent}s in your {@link ApplicationScreen}s. 
 * Wrapper around SmartGWT's {@link Layout}.
 * 
 * @author <a href="mailto:ice.penov@devmindset.com">Ice Penov</a>
 */
public class LegoPlaceholder extends Layout {
	
	private boolean animated;
	private AnimationEffect effect = AnimationEffect.FADE;
  
  /**
   * Constructor. 
   */
  public LegoPlaceholder() {
    ;
  }
  
  /**
   * Make an animated placeholder with the default FADE effect.
   * @param animated
   */
  public LegoPlaceholder(boolean animated) {
  	this.animated = animated;
  }
  
  /**
   * Make an animated placeholder using the specified effect.
   * 
   * @param effect
   */
  public LegoPlaceholder(AnimationEffect effect) {
  	this.animated = true;
  	this.effect = effect;
  }
    
  /**
   * Places a {@link Canvas} onto this placeholder.
   * 
   * Prior the placement any previous content is removed, and after the placement the
   * placeholder is refreshed.
   * 
   * @param canvas the {@link Canvas} to be placed
   */
  public void place(final Canvas canvas) {
	  if (canvas instanceof Window) {
		  canvas.show();
	  } else {
		  if (!contains(canvas)) {
			  removeMembers(getMembers());
			  addMember(canvas);
		  }
		  markForRedraw();			  		  
	  }
  }
  
  /**
   * Does the same as this{@link #place(Canvas)}, only in a animated fashion.
   * 
   * @param canvas the canvas to be placed
   */
  public void placeAnimated(final Canvas canvas) {
    setAnimateMembers(false);
    if (canvas instanceof Window) {     
      Timer timer = new Timer() {
        public void run() {
          canvas.animateShow(effect, new AnimationCallback() {
            public void execute(boolean earlyFinish) {
              canvas.show();
            }
          });                        
        }
      };
      timer.schedule(1000);
    } else {
      Canvas[] c = getMembers();
      if (c != null && c.length == 1) {
        final Canvas p = c[0];
        animateHide(effect, new AnimationCallback() {
          public void execute(boolean earlyFinish) {
              removeMember(p);
              animateShow(effect, new AnimationCallback() {
                public void execute(boolean earlyFinish) {
                  addMember(canvas);
                  markForRedraw();                                           
                };
              });
          }          
        });
      } else {
        removeMembers(getMembers());
        addMember(canvas);
        markForRedraw();              
      }
    }
	  
  }

	public boolean isAnimated() {
		return animated;
	}
  
}